#!/usr/bin/env python
#
# Copyright 2008 Gee-Hsien Chuang
# GNU General Public License v3
# http://www.gnu.org/licenses/gpl.html
#

import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

from iknow import iKnow

class MainHandler(webapp.RequestHandler):

	def get(self):
		iknow = iKnow()
		profile = iknow.user_profile("geechuang")
		
		contacts = iKnow.find_children(profile,'foaf:knows')
		numcontacts = len(contacts)
		contact_names = []
		for contact in contacts:
			person = iKnow.find_children(contact,'foaf:Person')[0]
			contact_names.append(iKnow.find_children(person,'foaf:name')[0]["nodeValue"]["value"])
			
		path = 'main.html'
		template_values = {
			'avatar': iKnow.find_children(profile,'foaf:depiction')[0]["nodeValue"]["attributes"]["rdf:resource"],
			'name': iKnow.find_children(profile,'foaf:name')[0]["nodeValue"]["value"],
			'gender': iKnow.find_children(profile,'foaf:gender')[0]["nodeValue"]["value"],
			'homepage': iKnow.find_children(profile,'foaf:homepage')[0]["nodeValue"]["attributes"]["rdf:resource"],
			'numcontacts': numcontacts,
			'contacts': contact_names
		}

		self.response.out.write(template.render(path, template_values))

def main():
	application = webapp.WSGIApplication([('/', MainHandler)],
											debug=True)
	wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
	main()
